/**
 * 
 */
package com.android.hw.projectmenu;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemProperties;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import com.qualcomm.qcnvitems.QcNvItems;
import com.android.internal.telephony.Phone;
import com.android.internal.telephony.PhoneFactory;
import android.util.Log;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
/**
 * @author gaoshengpan
 *
 */
public class HardwareAndSoftwareInfo extends PreferenceActivity {
	private static final String KEY_PCB_INFO = "pcb_info";
    private static final String KEY_EXTERNALSOFT_INFO = "external_soft_info";
    private static final String KEY_INTERNALSOFT_INFO = "internal_soft_info";
    private static final String KEY_EXTERNALHAREWARE_INFO = "external_hareware_info";
    private static final String KEY_INTERNALHAREWARE_INFO = "internal_hardware_info";
    private static final String KEY_ESN_NUM = "esn_num";
    private static final String KEY_MEID_NUM = "meid_num";
    private static final String KEY_IMEI_NUM = "imei_num";
    private static final String KEY_MAC_NUM = "mac_num";
    private static final String KEY_SN_NUM = "sn_num";
    private static final String KEY_QCN_VERSION = "qcn_version";
    private static String mUnknown = "null";
    private Phone mPhone = null;
    private QcNvItems mNv;
    private String snNUm = "";
    private WifiManager wifiManager;
    private WifiInfo wifiInfo;
    
    private String TAG = "HardwareAndSoftwareInfo";

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            switch (msg.what) {

            case 0:

                break;
            case 1:
            	/*15-10-23 gaoshenpan fix snNUm null exception begin */
            	snNUm = readSN() == null ? mUnknown : readSN().substring(0, 31);
            	/*15-09-01 gaoshenpan  fix sn_number begin */
                setSummaryText(KEY_SN_NUM, snNUm);
                /*15-10-23 gaoshenpan fix snNUm null exception end */
                /*15-09-01 gaoshenpan  fix sn_number end */
                // setSummaryText(KEY_IMEI_MEID, getImeiMeid());
                break;
            case 2:
                try {
            		setSummaryText(KEY_QCN_VERSION, mNv.getQcnVersion());
        		} catch (Exception e) {
        			// TODO: handle exception
        			Log.d(TAG, "KEY_QCN_VERSION-----error");
        		}
            	break;
            default:
                break;
            }
        }
    };

    /**
     * 
     */
    public HardwareAndSoftwareInfo() {
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.hard_soft_info);
        int numPhone = TelephonyManager.getDefault().getPhoneCount();
        Log.d(TAG, "phoneCount---------->" + numPhone);
        mPhone = PhoneFactory.getPhone(0);
        wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
        wifiInfo = wifiManager.getConnectionInfo();
        mNv = new QcNvItems(this);
        if (mPhone != null) {
            Log.d(TAG, "phone---------->notNull" + numPhone);
        } else {
            Log.d(TAG, "phoneType---------->null" + numPhone);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onResume()
     */
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        setPreferenceSummary();
    }

    public void setPreferenceSummary() {
        TelephonyManager mtm = TelephonyManager.getDefault();
        setSummaryText(KEY_PCB_INFO,
        		readFile("/sys/devices/soc0/hw_pcbboard_version"));

        setSummaryText(KEY_EXTERNALSOFT_INFO,
                SystemProperties.get("ro.build.display.id", mUnknown));
       /* gaoshenpan 15-8-8 fix remove hy_internel_version begin*/
        /*setSummaryText(KEY_INTERNALSOFT_INFO,
                SystemProperties.get("ro.build.inter_version", mUnknown));*/
        /* gaoshenpan 15-8-8 fix remove hy_internel_version end*/
        setSummaryText(KEY_INTERNALHAREWARE_INFO,
                SystemProperties.get("ro.hw_version", mUnknown));
        setSummaryText(KEY_IMEI_NUM, mtm.getDeviceId(1));
        if (mPhone != null) {
            Log.d(TAG, "set---------->" + KEY_ESN_NUM);
            setSummaryText(KEY_ESN_NUM, mPhone.getEsn());
            setSummaryText(KEY_MEID_NUM, mPhone.getMeid());

            getSN();
        }
        mHandler.sendEmptyMessage(2);
        setSummaryText(KEY_MAC_NUM, wifiInfo.getMacAddress());
    }

    public static String readFile(String filePath) {
		String res = "";
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new FileInputStream(new File(filePath))));
			String str = null;
			while ((str = br.readLine()) != null) {
				res += str;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
    public String getSN() {
        String info = null;
        if (mNv == null) {
            mNv = new QcNvItems(this);
        }
        if (mNv != null) {
            mHandler.sendEmptyMessage(1);
        }

        if (info == null) {
            return mUnknown;
        }
        return info;
    }

    private String readSN() {
        String sn = null;
        try {
        	/*15-09-08 gaoshenpan fix just show hwSN begin*/
            sn = mNv.getSNNumber1();
            /*15-09-08 gaoshenpan fix just show hwSN end*/
            Log.d(TAG, "readSN -----:" + sn);
        } catch (Exception e) {
            // TODO: handle exception
            Log.e(TAG, "getSN e:" + e);
        }
        Log.e(TAG, "getSN info :" + sn);
        return sn;
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.preference.PreferenceActivity#onDestroy()
     */
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if (mNv != null) {
            mNv.dispose();
        }
    }

    private void setSummaryText(String preferenceKey, String value) {
        Preference preference = findPreference(preferenceKey);
        if (preference == null)
            return;

        if (TextUtils.isEmpty(value))
            preference.setSummary(mUnknown);
        else
            preference.setSummary(value);
    }
}
